<?php

class Bootstrap {
	
	
	public function __construct() 
	{

		
        $url = isset($_GET['url']) ? $_GET['url'] : NULL ;
		
        
        if(is_null($url))
        {
            require 'controllers/index.php' ;
            $controller = new Index();
            $controller->index();
            return false;
        }
        
        
		/**
		 *
		 * @todo validate $url ( Security )
		 */
		
		$parts = explode(DS, rtrim($url, DS));
		
		
		/**
		 *  require class and method
		 *
		 *  /any.com/index/show
		 *
		 *
		 *  index - the name controller
		 *  show  - the name method in controller
		 *
		 *
		*/
		
		
		$file =  'controllers/' . $parts[0] . '.php';
		
		
		if (file_exists($file))
		{
			require $file;
		} else {
			
			
			require 'controllers/error.php';
			$controller = new Error();
			
			
			// do exit
			return false;
			
		}
		
		
		$controller = new $parts[0];
        $controller->loadModel($parts[0]);
		
		
		/**
		 *  if we have controller and method and argument
		 *  for excemple  : www.any.com\index\show\123
		 */
		if(isset($parts[2]))
		{
			$controller->{$parts[1]}($parts[2]);
		} 
            else 
        {
           
            if(isset($parts[1]))
            {
                $controller->{$parts[1]}();
            } 
             
        }
        
        $controller->index();
		
	}
}